<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
	/**
     * @var Zend_Log
     */
    protected $_logger;

    /**
     * @var Zend_Application_Module_Autoloader
     */
    protected $_resourceLoader;

    /**
     * @var Zend_Controller_Front
     */
    public $frontController;
	
/**
     * Setup the logging
     */
    protected function _initLogging()
    {
        $this->bootstrap('frontController');
        $logger = new Zend_Log();

        $writer = 'production' == $this->getEnvironment() ?
			new Zend_Log_Writer_Stream(APPLICATION_PATH . '/../data/logs/app.log') :
			new Zend_Log_Writer_Firebug();
        $logger->addWriter($writer);

		if ('production' == $this->getEnvironment()) {
			$filter = new Zend_Log_Filter_Priority(Zend_Log::CRIT);
			$logger->addFilter($filter);
		}

        $this->_logger = $logger;
        Zend_Registry::set('log', $logger);
    }
    
 	/**
     * Configure the default modules autoloading, here we first create
     * a new module autoloader specifiying the base path and namespace
     * for our default module. This will automatically add the default
     * resource types for us. We also add two custom resources for Services
     * and Model Resources.
     */
    protected function _initDefaultModuleAutoloader()
    {
        $this->_logger->info('Bootstrap ' . __METHOD__);
        
        $this->_resourceLoader = new Zend_Application_Module_Autoloader(array(
            'namespace' => 'Eleven',
            'basePath'  => APPLICATION_PATH,
        ));
        $this->_resourceLoader->addResourceTypes(array(
            'modelResource' => array(
              'path'      => 'models/resources',
              'namespace' => 'Resource',
            ),
            'service' => array(
              'path'      => 'services',
              'namespace' => 'Service',
            ),
        ));
        
        //Zend_Debug::dump($this->_resourceLoader);exit;
    }
    
	protected function _initLocate()
	{
		$this->_logger->info('Bootstrap ' . __METHOD__);
		
		$locate = new Zend_Locale('zh_CN');
		Zend_Registry::set('Zend_Locale', $locate);		
	}
	
	/**
     * Setup the database profiling
     */
    protected function _initDbProfiler()
    {
        $this->_logger->info('Bootstrap ' . __METHOD__);
        
        if ('production' !== $this->getEnvironment()) {
            $this->bootstrap('db');
            $profiler = new Zend_Db_Profiler_Firebug('All DB Queries');
            $profiler->setEnabled(true);
            $this->getPluginResource('db')->getDbAdapter()->setProfiler($profiler);
        }
    }
    
	protected function _initView()
	{
		$this->_logger->info('Bootstrap ' . __METHOD__);
		
		$view = new Zend_View();
		$view->doctype('XHTML1_STRICT');
		$view->headTitle('北京第十一中学国际交流圈');
		$view->skin = 'blues';
		
		$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
		$viewRenderer->setView($view);
		
		return $view;
	}
	
 	/**
     * Add required routes to the router
     */
    protected function _initRoutes()
    {
        $this->_logger->info('Bootstrap ' . __METHOD__);
        $this->bootstrap('frontController');

        $router = $this->frontController->getRouter();

        $route = new Zend_Controller_Router_Route(
            'announcementView/:announcementId',
            array(
                'action'        => 'view',
                'controller'    => 'announcement',
            	'announcementId'	=> '',
            ),
            array(
                'announcementId'  => '\d+'
            )
        );

        $router->addRoute('announcement_content', $route);

        $route = new Zend_Controller_Router_Route(
            'announcement/:page',
            array(
                'action'        => 'index',
                'controller'    => 'announcement',
                'page'          => 1
            ),
            array(
                'page'          => '\d+'
            )
        );

        $router->addRoute('announcement', $route);
        
        $route = new Zend_Controller_Router_Route(
            'aannouncement/:page',
            array(
                'action'        => 'index',
                'controller'    => 'aannouncement',
                'page'          => 1
            ),
            array(
                'page'          => '\d+'
            )
        );

        $router->addRoute('aannouncement', $route);

        $route = new Zend_Controller_Router_Route(
            'teacherView/:contentId/:replyPageId',
            array(
                'action'        => 'view',
                'controller'    => 'teacher',
            	'contentId'	    => '',
                'replyPageId'	=> '',
            ),
            array(
                'contentId'     => '\d+',
                'replyPageId'	=> '\d+'
            )
        );

        $router->addRoute('teacher_content', $route);
        
        $route = new Zend_Controller_Router_Route(
            'teacher/:topicId/:page',
            array(
                'action'        => 'index',
                'controller'    => 'teacher',
                'topicId'		=> '',
                'page'          => 1,
            ),
            array(
                'topicId'		=> '\d+',
                'page'          => '\d+',
            )
        );

        $router->addRoute('teacher', $route);
        
        $route = new Zend_Controller_Router_Route(
            'studentView/:contentId/:replyPageId',
            array(
                'action'        => 'view',
                'controller'    => 'student',
            	'contentId'	    => '',
                'replyPageId'	=> '',
            ),
            array(
                'contentId'     => '\d+',
                'replyPageId'	=> '\d+'
            )
        );

        $router->addRoute('student_content', $route);
        
        $route = new Zend_Controller_Router_Route(
            'student/:topicId/:page/:type',
            array(
                'action'        => 'index',
                'controller'    => 'student',
                'topicId'		=> '',
                'page'          => 1,
            	'type'			=> 's',
            ),
            array(
                'topicId'		=> '\d+',
                'page'          => '\d+',
            	'type'			=> '\w+',
            )
        );

        $router->addRoute('student', $route);
        
        $route = new Zend_Controller_Router_Route(
            'picture/:page',
            array(
                'action'        => 'index',
                'controller'    => 'picture',
                'page'          => 1
            ),
            array(
                'page'          => '\d+'
            )
        );

        $router->addRoute('picture', $route);
        
        $route = new Zend_Controller_Router_Route(
            'apicture/:page',
            array(
                'action'        => 'index',
                'controller'    => 'apicture',
                'page'          => 1
            ),
            array(
                'page'          => '\d+'
            )
        );

        $router->addRoute('apicture', $route);
        
        $route = new Zend_Controller_Router_Route(
            'admin/:controller/:action/*',
            array(
                'action'        => 'index',
                'controller'    => 'aintroduction',
                'isAdmin'		=> true
            ));

        $router->addRoute('admin', $route);
    }

}

